package com.gov.dna.common.util;

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

/**
 * 代码生成器工具类
 *
 * @author (作者) Dong Jie 154046519@qq.com
 * @version V1.0
 * @date 2019/1/4 15:52
 * @since (该版本支持的JDK版本) 1.8
 */
public class CodeGeneratorUtils {

    public static void main(String[] args) {
        List<Field> list = getFieldsByClass(Test.class);
        System.out.println(list);
    }

    /**
     * 根据类构建字段属性
     *
     * @param cc
     * @return java.util.List<com.gov.dna.code.entity.Field>
     * @author (作者) Dong Jie 154046519@qq.com
     * @date 2019/1/4 13:34
     */
    public static List<Field> getFieldsByClass(Class cc) {
        List<Field> list = new ArrayList<Field>();
        List<Field> result = null;
        while (cc != null) {
            List<Field> tempList = new ArrayList<Field>();
            java.lang.reflect.Field[] fields = cc.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field f = buildField(fields[i]);
                if (null != f) {
                    tempList.add(f);
                }
            }
            if (list.size() > 0) {
                tempList.addAll(list);
                result = tempList;
                list=tempList;
            } else {
                list.addAll(tempList);
                result = list;
            }
            cc = cc.getSuperclass();
            if (cc.equals(Model.class)) {
                cc = null;
            }
        }
        return result;
    }

    /**
     * 将entity的字段转换成Field
     *
     * @param field
     * @return com.gov.dna.code.entity.Field
     * @author (作者) Dong Jie 154046519@qq.com
     * @date 2019/1/4 15:51
     */
    private static Field buildField(java.lang.reflect.Field field) {
        Field f = new Field();
        if (field.getModifiers() == 2) {
            f.setJavaFieldName(field.getName());
            f.setJavaFieldType(field.getType().getSimpleName());
            f.setDbFieldName(GovStringUtils.underscoreName(field.getName()));
            f.setDbFieldType("VARCHAR(255)");
            if (f.getJavaFieldType().equals("Float")) {
                f.setDbFieldType("DECIMAL(10,2)");
            } else if (f.getJavaFieldType().equals("Long")) {
                f.setDbFieldType("BIGINT(20)");
            } else if (f.getJavaFieldType().equals("Integer")) {
                f.setDbFieldType("INT(11)");
            } else if (f.getJavaFieldType().equals("Boolean")) {
                f.setDbFieldType("TINYINT(1)");
            } else if (f.getJavaFieldType().equals("LocalDateTime")) {
                f.setDbFieldType("TIMESTAMP");
            } else if (!f.getJavaFieldType().equals("String")) {
                f.setDbFieldType("未处理的类型(" + f.getJavaFieldType() + ")");
            }
            Annotation[] annotations = field.getAnnotations();
            if (null != annotations) {
                for (Annotation annotation : annotations) {
                    if (annotation instanceof TableField) {
                        if (!((TableField) annotation).exist()) {
                            return null;
                        } else if (StringUtils.isNotEmpty(((TableField) annotation).value())) {
                            f.setDbFieldName(((TableField) annotation).value());
                        }
                    } else if (annotation instanceof ApiModelProperty) {
                        f.setDescription(((ApiModelProperty) annotation).value());
                    } else if (annotation instanceof TableId) {
                        f.setKey(true);
                    }
                }
            }
        }else{
            return null;
        }
        return f;
    }
}
